package cn.xiaolang.function.string;

/**
 * string 之间共同过字符串
 *
 * @author fuhw/DeanKano
 * @date 2018-12-18
 */
public class StringBetweenJoin {
	/**
	 * 1 将短的那个子串按照长度递减的方式获取
	 * 2 将获取到的子串去长串中判断是否包含，如果包含，已经找到！
	 * @param s1
	 * @param s2
	 * @return
	 * @author fuhw/DeanKano
	 */
	public static String getMaxJoinBetweenStr(String s1, String s2) {
		String max = "", min = "";
		max = (s1.length() > s2.length()) ? s1 : s2;
		min = (max == s1) ? s2 : s1;
		// 循环短的字符串,如:abcde 5 x最大为4
		int minLen = min.length();
		for (int x = 0; x < minLen; x++) {
			/*
			 * x 固定了每次截取到字符串长度
			 * y 固定截取子字符串到开始角标
			 * z 固定了截取子字符串到结束角标 (z到最大值为minLen)
			 */
			for (int y = 0, z = minLen - x; z != minLen + 1; y++, z++) {
				// [0到未],[1,]
				String temp = min.substring(y, z);
//				System.out.println("x="+x+",y="+y+",z="+z);
				if (max.contains(temp)) {// if(s1.indexOf(temp)!=-1)
					printStr(temp);
					return temp;
				}
			}
		}
		return "";
	}

	public static void main(String[] args) {
		String s1 = "abhellobddd";
		String s2 = "cvhellobnm";
		getMaxJoinBetweenStr(s2, s1);
	}

	public static void printStr(String str) {
		System.out.println(str);
	}
}
